package com.zz.yt.test.delegate.news;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.whf.android.jar.base.latte.BaseActivity;
import com.whf.android.jar.constants.UserConstant;
import com.whf.android.jar.util.log.LatteLogger;
import com.whf.android.jar.util.storage.LattePreference;
import com.zz.yt.test.R;
import com.zz.yt.test.gloading.SerialActivity;

import org.jetbrains.annotations.NotNull;

import java.util.List;


/**
 * @author qf
 * @version 2023-04-23
 */
public class NewsActivity extends BaseActivity implements AdapterView.OnItemClickListener {


    private final String[] title = {
            "个人信息加密",
            "获取IMEI"
    };

    private final Class[] classes = {
            LoadingActivity.class,
            SerialActivity.class
    };

    ListView idListView;

    @Override
    protected Object setLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState) {
        addMarginTopEqualStatusBarHeight(findViewById(R.id.id_title));
        setTextView(R.id.id_title, "个人信息加密");
        initView();
    }


    private void initView() {
        idListView = findViewById(R.id.id_list_view);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                NewsActivity.this,
                android.R.layout.simple_list_item_1,
                title);
        idListView.setAdapter(adapter);
        idListView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0) {
            storagePermission();
            LattePreference.addCustomAppProfile(UserConstant.USER_USER, "李四");
            String name = LattePreference.getCustomAppProfile(UserConstant.USER_USER);
            LatteLogger.i(name);
            return;
        }
        ActivityUtils.startActivity(classes[position]);
    }


    /**
     * 获得更新de存储权限
     */
    private void storagePermission() {
        PermissionUtils
                .permission(PermissionConstants.STORAGE)
                .rationale((activity, shouldRequest) -> shouldRequest.again(true))
                .callback(new PermissionUtils.FullCallback() {
                    @Override
                    public void onGranted(@NotNull List<String> permissionsGranted) {
                        LatteLogger.d("权限获取成功", permissionsGranted);
                    }

                    @Override
                    public void onDenied(@NotNull List<String> permissionsDeniedForever,
                                         @NotNull List<String> permissionsDenied) {
                        LatteLogger.d("权限永久拒绝", permissionsDeniedForever);
                        LatteLogger.d("权限拒绝", permissionsDenied);
                    }
                })
                .theme(ScreenUtils::setFullScreen)
                .request();
    }
}

